<?php

namespace app\common\validate;

use think\Validate;

class Contact extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
    	'mobile' => 'require|length:11|number|checkMobile',
	    'remark' => 'require'
    ];
    /**
     * 提示消息
     */
    protected $message = [
    	'mobile' => '手机号码格式不正确',
	    'remqrk' => '请填写备注名称'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
    
    protected function checkMobile($value, $rule, $data) {
    	$pre = '/^1\d{10}$/';
    	if(!preg_match($pre, $value)) {
    		return '手机号码格式不正确';
	    }
	    
	    $where = [
	    	'mobile' => $data['mobile'],
		    'user_id' => $data['user_id']
        ];
    	
    	if(isset($data['id']) && $data['id']) {
    		$where['id'] = ['neq', $data['id']];
	    }
	    
	    $row = \app\common\model\Contact::get($where);
    	if($row) {
    		return '该手机号已添加过了';
	    }
	    
	    return true;
    }
    
}
